package com.h3c.nem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eos.common.util.IdWorker;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.h3c.nem.entity.TbWorkerGrade;
import com.h3c.nem.mapper.TbWorkerGradeMapper;
import com.h3c.nem.service.CommService;
import com.h3c.nem.service.ISdDeptService;
import com.h3c.nem.service.ITbWorkerGradeService;
import com.h3c.nem.dto.TbWorkerGradeDTO;
import com.h3c.nem.utils.BusiEnumDefine;
import com.h3c.nem.utils.InitExampleOrderByUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Copyright (C), 2020-2020, h3c
 * FileName: TbWorkerGradeServiceImpl
 *
 * @Author: l22886
 * @Date: 2022-02-23
 * Description: tb_worker_grade serviceImpl
 * Version: 1.0
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class TbWorkerGradeServiceImpl extends ServiceImpl<TbWorkerGradeMapper, TbWorkerGrade> implements ITbWorkerGradeService {

    @Resource
    private TbWorkerGradeMapper tbWorkerGradeMapper;
    @Resource
    private CommService commService;
    @Resource
    private ISdDeptService deptService;

    /**
     * @title: selectFuzzyByDto
     * @description: 模糊查询操作
     * @params: [ dto ]
     * @return: java.util.List
     * @createTime: 2022-02-23
     * @version: 1.0
     * @author: l22886
     */
    @Override
    public List<TbWorkerGradeDTO> selectFuzzyByDto(TbWorkerGradeDTO dto) {
        //List<TbWorkerGrade> list = tbWorkerGradeMapper.selectByExample(initFuzzyExample(dto));
        List<TbWorkerGrade> list = tbWorkerGradeMapper.queryListByFuzzy(JSONObject.parseObject(JSONObject.toJSONString(dto),TbWorkerGrade.class));
        return JSONArray.parseArray(JSONArray.toJSONString(list), TbWorkerGradeDTO.class);
    }

    /**
     * @title: selectByDto
     * @description: 精确查询操作
     * @params: [ dto ]
     * @return: java.util.List
     * @createTime: 2022-02-23
     * @version: 1.0
     * @author: l22886
     */
    @Override
    public List<TbWorkerGradeDTO> selectByDto(TbWorkerGradeDTO dto) {
        //List<TbWorkerGrade> list = tbWorkerGradeMapper.selectByExample(initExample(dto));
        List<TbWorkerGrade> list = tbWorkerGradeMapper.queryList(JSONObject.parseObject(JSONObject.toJSONString(dto),TbWorkerGrade.class));
        return JSONArray.parseArray(JSONArray.toJSONString(list), TbWorkerGradeDTO.class);
    }

    /**
     * @title: insertDto
     * @description: 插入操作
     * @params: [ dto ]
     * @return: Integer
     * @createTime: 2022-02-23
     * @version: 1.0
     * @author: l22886
     */
    @Override
    public Integer insertDto(TbWorkerGradeDTO dto) {
        //if (null == dto.getId() || "".equals(dto.getId())) {
        //    String pkId = idWorker.nextId() + "";
        //    dto.setId(pkId);
        //}
        return tbWorkerGradeMapper.insertTbWorkerGrade(JSONObject.parseObject(JSON.toJSONString(dto), TbWorkerGrade.class));
    }

    /**
     * @title: updateDto
     * @description: 修改操作
     * @params: [ dto ]
     * @return: Integer
     * @createTime: 2022-02-23
     * @version: 1.0
     * @author: l22886
     */
    @Override
    public Integer updateDto(TbWorkerGradeDTO dto) {
        //if (null != dto && null != dto.getId() && !"".equals(dto.getId())) {
            return tbWorkerGradeMapper.updateTbWorkerGrade(JSONObject.parseObject(JSON.toJSONString(dto), TbWorkerGrade.class));
            //return tbWorkerGradeMapper.updateByPrimaryKey(JSONObject.parseObject(JSON.toJSONString(dto), TbWorkerGrade.class));
        //} else {
        //    return 0;
        //}
    }

    /**
     * @title: deleteDto
     * @description: 删除操作
     * @params: [ dto ]
     * @return: Integer
     * @createTime: 2022-02-23
     * @version: 1.0
     * @author: l22886
     */
    @Override
    public Integer deleteDto(TbWorkerGradeDTO dto) {
        //if (null != dto && null != dto.getId() && !"".equals(dto.getId())) {
            return tbWorkerGradeMapper.deleteTbWorkerGrade(JSONObject.parseObject(JSON.toJSONString(dto), TbWorkerGrade.class));
            //return tbWorkerGradeMapper.deleteByPrimaryKey(dto.getId());
        //} else {
        //    return 0;
        //}
    }

    @Override
    public Map<String, Object> getWorkerGrade() {
        boolean adminAuth = commService.checkLoginUserRole(BusiEnumDefine.USER_ROLE_ADMIN);
        boolean bpAuth =  commService.checkLoginUserBP();
        boolean conAuth =  commService.checkLoginUserRole(BusiEnumDefine.USER_ROLE_DEPT_CONTACTOR);
        boolean manAuth =  commService.checkLoginUserRole(BusiEnumDefine.USER_ROLE_DEPT_MANAGER);
        Set<Integer> deptIds = null;
        if (adminAuth) {
        }else if (bpAuth || conAuth || manAuth){
            deptIds = deptService.getAuthDeptIds(bpAuth,conAuth,manAuth,false);
        }else{
            Map<String,Object> resultMap = new HashMap<>();
            resultMap.put("GradeA",new LinkedList<>());
            resultMap.put("GradeC",new LinkedList<>());
            return resultMap;
        }
        Date now = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        calendar.add(Calendar.MONTH,-1);
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMM");
        String gradeTime = sf.format(calendar.getTime());
        PageHelper.startPage(1,10);
        List<TbWorkerGradeDTO> AGradeWorkers = tbWorkerGradeMapper.getWorkersByGrade(deptIds,gradeTime,"A","A+");
        PageHelper.startPage(1,10);
        List<TbWorkerGradeDTO> CDGradeWorkers = tbWorkerGradeMapper.getWorkersByGrade(deptIds,gradeTime,"C","D");
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("GradeA",AGradeWorkers);
        resultMap.put("GradeC",CDGradeWorkers);
        return resultMap;
    }


    /**
     * @title: initFuzzyExample
     * @description: 初始化 模糊查询 example
     * @params: [ dto ]
     * @return: tk.mybatis.mapper.entity.Example
     * @createTime: 2022-02-23
     * @version: 1.0
     * @author: l22886
     */
    private Example initFuzzyExample(TbWorkerGradeDTO dto) {
        Example example = InitExampleOrderByUtils.initExampleOrderBy(TbWorkerGrade.class, dto.getOrderBy());
        Example.Criteria criteriaName = example.createCriteria();
        if (null != dto) {
            if (null != dto.getWorkerId() && !"".equals(dto.getWorkerId())) {
                criteriaName.andLike("WorkerId", "%" + dto.getWorkerId() + "%");
            }
            if (null != dto.getWorkerName() && !"".equals(dto.getWorkerName())) {
                criteriaName.andLike("WorkerName", "%" + dto.getWorkerName() + "%");
            }
            if (null != dto.getScoreMonth() && !"".equals(dto.getScoreMonth())) {
                criteriaName.andLike("ScoreMonth", "%" + dto.getScoreMonth() + "%");
            }
            if (null != dto.getWorkerGrade() && !"".equals(dto.getWorkerGrade())) {
                criteriaName.andLike("WorkerGrade", "%" + dto.getWorkerGrade() + "%");
            }
            if (null != dto.getWorkerEvaluate() && !"".equals(dto.getWorkerEvaluate())) {
                criteriaName.andLike("WorkerEvaluate", "%" + dto.getWorkerEvaluate() + "%");
            }
            if (null != dto.getStartDate() && !"".equals(dto.getStartDate())) {
                criteriaName.andLike("StartDate", "%" + dto.getStartDate() + "%");
            }
            if (null != dto.getEndDate() && !"".equals(dto.getEndDate())) {
                criteriaName.andLike("EndDate", "%" + dto.getEndDate() + "%");
            }
            if (null != dto.getEvaluatorId() && !"".equals(dto.getEvaluatorId())) {
                criteriaName.andLike("EvaluatorId", "%" + dto.getEvaluatorId() + "%");
            }
            if (null != dto.getEvaluateTime() && !"".equals(dto.getEvaluateTime())) {
                criteriaName.andLike("EvaluateTime", "%" + dto.getEvaluateTime() + "%");
            }
        }
        return example;
    }

    /**
     * @title: initExample
     * @description: 初始化 精确查询 example
     * @params: [ dto ]
     * @return: tk.mybatis.mapper.entity.Example
     * @createTime: 2022-02-23
     * @version: 1.0
     * @author: l22886
     */
    private Example initExample(TbWorkerGradeDTO dto) {
        Example example = InitExampleOrderByUtils.initExampleOrderBy(TbWorkerGrade.class, dto.getOrderBy());
        Example.Criteria criteriaName = example.createCriteria();
        if (null != dto) {
            if (null != dto.getWorkerId() && !"".equals(dto.getWorkerId())) {
                criteriaName.andEqualTo("WorkerId", dto.getWorkerId());
            }
            if (null != dto.getWorkerName() && !"".equals(dto.getWorkerName())) {
                criteriaName.andEqualTo("WorkerName", dto.getWorkerName());
            }
            if (null != dto.getScoreMonth() && !"".equals(dto.getScoreMonth())) {
                criteriaName.andEqualTo("ScoreMonth", dto.getScoreMonth());
            }
            if (null != dto.getWorkerGrade() && !"".equals(dto.getWorkerGrade())) {
                criteriaName.andEqualTo("WorkerGrade", dto.getWorkerGrade());
            }
            if (null != dto.getWorkerEvaluate() && !"".equals(dto.getWorkerEvaluate())) {
                criteriaName.andEqualTo("WorkerEvaluate", dto.getWorkerEvaluate());
            }
            if (null != dto.getStartDate() && !"".equals(dto.getStartDate())) {
                criteriaName.andEqualTo("StartDate", dto.getStartDate());
            }
            if (null != dto.getEndDate() && !"".equals(dto.getEndDate())) {
                criteriaName.andEqualTo("EndDate", dto.getEndDate());
            }
            if (null != dto.getEvaluatorId() && !"".equals(dto.getEvaluatorId())) {
                criteriaName.andEqualTo("EvaluatorId", dto.getEvaluatorId());
            }
            if (null != dto.getEvaluateTime() && !"".equals(dto.getEvaluateTime())) {
                criteriaName.andEqualTo("EvaluateTime", dto.getEvaluateTime());
            }
        }
        return example;
    }


}
